package New;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
class MyResource{
    Map<String,String> map = new HashMap<>();
    Lock lock = new ReentrantLock();
//    读写互斥，读读共享
    ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    public void write(String key,String value){
        rwLock.writeLock().lock();
        try {
            System.out.println(Thread.currentThread().getName()+"\t"+"---正在写入");
            map.put(key,value);
            try{
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
                System.out.println(Thread.currentThread().getName()+"\t"+"---完成写入");
        }finally {
        rwLock.writeLock().unlock();
        }
    }
    public void read(String key){
        rwLock.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName()+"\t"+"----正在读取");
            String result = map.get(key);
            System.out.println(Thread.currentThread().getName()+"\t"+"---完成读取result:"+result);
        }finally {
         rwLock.readLock().unlock();
        }
    }
}
public class ReentrantReadWriteLockDemo {
    public static void main(String[] args) {
        MyResource myResource = new MyResource();
        for (int i = 1;i<=10;i++){
            int finalI = i;
            new Thread(()->{
                myResource.write(finalI+"",finalI+"");
            },String.valueOf(i)).start();
        }
        for (int i = 1;i<=10;i++){
            int finalI = i;
            new Thread(()->{
                myResource.read(finalI+"");
            },String.valueOf(i)).start();
        }
    }
}
